package com.example.demo.controller;

import com.example.demo.pojo.Lend;
import com.example.demo.pojo.PageBean;
import com.example.demo.pojo.QueryCriteria;
import com.example.demo.pojo.TokenData;
import com.example.demo.service.LendService;
import com.example.demo.utils.Result;
import com.example.demo.utils.ResultFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 借阅Controller
 */
@RestController
@CrossOrigin
@RequestMapping("/lend")
public class LendController{

    @Autowired
    private LendService lendService;

    private Logger logger = LoggerFactory.getLogger(LendController.class);


    /**
     * 图书预约
     */

    @GetMapping("/reservationBookByBookId/{book_id}")
    public Result reservationBookByBookId(@PathVariable int book_id, HttpSession session){

        TokenData currentUser = (TokenData) session.getAttribute("currentUser");
        Result result = null;
        if(currentUser.getState() == 2){ //借阅证暂停
            result = ResultFactory.failureWithStatus(999,"借阅证已被暂停,请求前台查看原因后再来预约");
        }else {
            result = this.lendService.reservationBookByBookId(book_id, currentUser.getReader_id());
        }
        return result;

    }

    /**
     * 预约记录列表展示
     */
    @GetMapping("/reservationItemPage")
    public Result reservationItemPage(@RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue = "3") int pageSize){
        return this.lendService.queryReservationList(currentPage,pageSize);
    }

    /**
     * 图书借阅
     */
    @GetMapping("/lendBookByBookId/{book_id}")
    public Result lendBookByBookId(@PathVariable int book_id, HttpSession session){
        TokenData currentUser = (TokenData) session.getAttribute("currentUser");
        Result result = this.lendService.lendBookByBookId(book_id, currentUser.getReader_id());
        return result;
    }

    /**
     * 根据借阅id 实现 图书归还
     */
    @GetMapping("/returnBookBySerNum/{ser_num}")
    public Result returnBookBySerNum(@PathVariable int ser_num, HttpSession session){
        TokenData currentUser = (TokenData) session.getAttribute("currentUser");
        Result result = this.lendService.returnBookByBookId(ser_num, currentUser.getReader_id());
        if(result == null){
            result = ResultFactory.failureWithStatus(999,"图书归还失败");
        }
        return result;
    }

    /**
     * 借阅记录查询
     */
    @GetMapping("/lendItemPage")
    public Result lendItemPage(@RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue = "3") int pageSize){
        PageBean<Lend> lendPageBean = this.lendService.queryLendList(currentPage, pageSize);
        return ResultFactory.successWithStatusAndData(200,"分页显示借阅记录",lendPageBean);
    }

    /**
     * 读者借阅记录查询
     */
    @GetMapping("/lendItemPageByReader")
    public Result lendItemPageByReader(@RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue = "3") int pageSize,HttpSession session){
        TokenData currentUser = (TokenData) session.getAttribute("currentUser");
        PageBean<Lend> lendPageBean = this.lendService.queryLendListByList(currentPage, pageSize,currentUser.getReader_id());
        return ResultFactory.successWithStatusAndData(200,"分页显示借阅记录",lendPageBean);
    }


    /**
     * 根据条件查询借阅记录
     */
    @GetMapping("/lendItemPageByConditions")
    public Result lendItemPageByConditions(@RequestParam(defaultValue = "1") int currentPage, @RequestParam(defaultValue = "3") int pageSize, QueryCriteria queryCriteria){
        return this.lendService.queryLendListByQueryCriteria(currentPage,pageSize,queryCriteria);
    }

    /**
     * 交纳罚金
     */
    @GetMapping("/payAfinebyser_num")
    public Result payFine(int ser_num){
        return this.lendService.payFine(ser_num);
    }

    /**
     * 罚款历史查询
     */
    @GetMapping("/fineHistory")
    public Result fineHistory(@RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue = "3") int pageSize,HttpSession session){
        //管理员
        PageBean<Lend> lendPageBean = this.lendService.fineHistory(currentPage, pageSize);
        if(lendPageBean == null){
            return  ResultFactory.failureWithStatus(999,"没有罚款信息");
        }
        return ResultFactory.successWithStatusAndData(200,"分页显示罚款历史记录",lendPageBean);
    }

    /**
     * 图书续借
     */
    @GetMapping("/renewBookBySerNum/{ser_num}")
    public Result renewBookBySerNum(@PathVariable  int ser_num,HttpSession session){

        TokenData currentUser = (TokenData) session.getAttribute("currentUser");

        return  this.lendService.renewBookBySerNum(ser_num,currentUser.getReader_id());
    }
}
